package viewbuilder.support

import presenters.test.AssertableBuilder
import presenters.test.TestNode
import Post
import groovy.xml.MarkupBuilder
import Tag
import BlogConfig

class ViewBuilderExecuterTest extends GroovyTestCase {

    public void testScriptFileCanBeExecutedOnBuilder() {

        Post.metaClass.id = null
        def post1 = new Post(title:"A Title",
              teaser:"A Teaser",
              content:"Some Content",
              lastUpdated:new Date())
        def post2 = new Post(title:"Second Title",
              teaser:"Second Teaser",
              content:"Some Content",
              lastUpdated:new Date())

        def posts = [post1,post2]

        def request = new Expando()
        def session = new Expando()
        request.session = session
        def builder = new AssertableBuilder()
        def executer = new ViewBuilderExecuter(builder:builder,
                componentsLocation:"src/groovy/presenters/component",
                componentsPackage:"presenters.component")
        executer.execute("web-app/WEB-INF/viewbuilders/ListPostViewBuilder.groovy",
                [posts:posts,
                        tags:[new Tag(name:"foo")],
                        blogConfig:new BlogConfig(title:"title", description:"desc")],
                request, null)

        println(builder.nodesCalled)
        assertTrue(builder.nodesCalled.contains(new TestNode("div", [id:"post${post1.underscoredTitle}", 'class':'postcontainer'])))
        assertTrue(builder.nodesCalled.contains(new TestNode("div", [id:"post${post2.underscoredTitle}", 'class':'postcontainer'])))        
        println("looking for match to ${new TestNode("a", [href:'/groovypublish/post/list'], "title")}")

        assertTrue(builder.hasNodeWith(name:"a", value:"title"))
        assertTrue(builder.hasNodeWith(name:"p", value:"desc"))
    }
}